package com.ruoyi.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.domain.Commodity;
import com.ruoyi.mapper.CommodityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.BrandMapper;
import com.ruoyi.domain.Brand;
import com.ruoyi.service.IBrandService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 品牌Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-07
 */
@Service
public class BrandServiceImpl   extends ServiceImpl<BrandMapper, Brand> implements IBrandService
{
    @Resource
    private BrandMapper brandMapper;
    @Resource
    private CommodityMapper commodityMapper;
    /**
     * 查询品牌
     *
     * @param id 品牌主键
     * @return 品牌
     */
    @Override
    public Brand selectBrandById(Long id)
    {
        return brandMapper.selectBrandById(id);
    }

    /**
     * 查询品牌列表
     *
     * @param brand 品牌
     * @return 品牌
     */
    @Override
    public List<Brand> selectBrandList(Brand brand)
    {
        return brandMapper.selectBrandList(brand);
    }

    /**
     * 新增品牌
     *
     * @param brand 品牌
     * @return 结果
     */
    @Override
    public int insertBrand(Brand brand)
    {
        brand.setCreateTime(DateUtils.getNowDate());
        brand.setCreateBy(ShiroUtils.getLoginName());
        return brandMapper.insertBrand(brand);
    }

    /**
     * 修改品牌
     *
     * @param brand 品牌
     * @return 结果
     */
    @Override
    public int updateBrand(Brand brand)
    {
        return brandMapper.updateBrand(brand);
    }

    /**
     * 批量删除品牌
     *
     * @param ids 需要删除的品牌主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteBrandByIds(String ids)
    {
        List<Commodity> brandId = commodityMapper.selectList(new QueryWrapper<Commodity>().eq("brand_id", ids));
        if(brandId.size()>0){
            return AjaxResult.error("有相关联的商品不能删除");
        }

        return brandMapper.deleteBrandByIds(Convert.toStrArray(ids))>0?AjaxResult.success():AjaxResult.error();

    }

    /**
     * 删除品牌信息
     *
     * @param id 品牌主键
     * @return 结果
     */
    @Override
    public int deleteBrandById(Long id)
    {
        return brandMapper.deleteBrandById(id);
    }
}
